package game.module.nn.logic;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.entity.PlayingRole;
import game.module.nn.NnConstants;
import game.module.nn.bean.NnRound;
import game.session.GlobalTimer;
import game.session.SessionManager;
import io.netty.util.Timeout;
import io.netty.util.TimerTask;
import lion.netty4.message.GamePlayer;
import lion.netty4.message.INetty4EventHandler;
import lua.LuaMessageNn.PushNnRoomDissmiss;

public class NnDissmissRoomManager implements INetty4EventHandler{

	public static final Logger logger = LoggerFactory.getLogger(NnDissmissRoomManager.class);
	
	private GlobalTimer globalTimer = GlobalTimer.getInstance();
	
	private Map<String ,MapValueHolder> roomTimeoutMap =new ConcurrentHashMap<String, MapValueHolder>();
	
    public static final int SESSION_TIMEOUT_DELAY = NnConstants.AUTODISSMISSROOMTIME;// 150秒
	
	static class SingletonHolder {
		static NnDissmissRoomManager instance = new NnDissmissRoomManager();
	}

	public static NnDissmissRoomManager getInstance() {
		return SingletonHolder.instance;
	}
	public void advanceCancle(Integer playerId) {
		final NnRound nnround = NnDataManager.getInstance().getNnRoom(playerId);
		if(nnround == null) 
			return;
		cancleDissmiss(nnround.getRoomUuid());
		List<Integer> playerList =  nnround.getPlayerIds();
		PushNnRoomDissmiss PushMsg = new PushNnRoomDissmiss();
		PushMsg.index =(byte) playerList.indexOf(playerId);
		PushMsg.state = 1;
		List<Integer> playerIds = nnround.getPlayerIds();
		for(Integer playerid:playerIds) {
			PlayingRole pr = SessionManager.getInstance().getPlayer(playerid);
			if (pr != null && pr.isChannelActive()) {
				pr.writeAndFlush(PushMsg.build(pr.alloc()));
				// 推送关于出牌的信息
			}
		//	NnDataManager.getInstance().removePlayer(playerid);
		}
		NnLogic.getInstance().roundOver(nnround);
	}
	
	//取消定时任务
	public void cancleDissmiss(final String roomid) {
		if (!roomTimeoutMap.containsKey(roomid)) {
			return ;
		}
		MapValueHolder mapValueHolder =roomTimeoutMap.get(roomid);
		Timeout curTimeout = mapValueHolder.getWheelTimeout();
		curTimeout.cancel();
		roomTimeoutMap.remove(roomid);
	}
	
	//创建定时任务
	
	public void createRemoveNnRound(final Integer playerId) {
		final NnRound nnRound = NnDataManager.getInstance().getNnRoom(playerId);
		if(nnRound ==null) {
			return;
		}
		if(roomTimeoutMap.containsKey(nnRound.getRoomUuid()))
			return;//存在任务
		Timeout curTimeout = globalTimer.newTimeout(new TimerTask() {
			
			@Override
			public void run(Timeout timeout) throws Exception {
				// TODO Auto-generated method stub
				logger.info("dissmiss nnroom by vote playerid={} ",playerId);
				MapValueHolder oldValueHolder = roomTimeoutMap.get(nnRound.getRoomUuid());
				NnRound nnround = oldValueHolder.getNnround();
				List<Integer> playerIds = nnround.getPlayerIds();
				PushNnRoomDissmiss PushMsg = new PushNnRoomDissmiss();
				PushMsg.index =(byte) playerIds.indexOf(playerId);
				PushMsg.state = 1;
				if(nnround!=null) {
					for (Integer aPlayerId : playerIds) {
						PlayingRole pr = SessionManager.getInstance().getPlayer(aPlayerId);
						if (pr != null && pr.isChannelActive()) {
							pr.writeAndFlush(PushMsg.build(pr.alloc()));
							// 推送关于出牌的信息
						}
						NnDataManager.getInstance().removePlayer(aPlayerId);
					}
				}
				roomTimeoutMap.remove(nnRound.getRoomUuid());
			}
		}, SESSION_TIMEOUT_DELAY);
		List<Integer> playerList =  nnRound.getPlayerIds();
		MapValueHolder mapValueHolder = new  MapValueHolder(curTimeout,nnRound);
		nnRound.setStartVoteTime(System.currentTimeMillis());
		nnRound.setVoteplayerIndex((byte) playerList.indexOf(playerId));
		roomTimeoutMap.put(nnRound.getRoomUuid(), mapValueHolder);
	}
	
	@Override
	public void channelInactive(GamePlayer gamePlayer) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void readTimeout(GamePlayer gamePlayer) {
		logger.info("read timeout,close socket!,addr={}", gamePlayer.getAddress());
		gamePlayer.close();
		
	}

}
class MapValueHolder {
	private Timeout wheelTimeout;
	private NnRound nnround;

	public MapValueHolder(Timeout wheelTimeout, NnRound sssround) {
		super();
		this.setWheelTimeout(wheelTimeout);
		this.setNnround(sssround);
	}

	public Timeout getWheelTimeout() {
		return wheelTimeout;
	}

	public void setWheelTimeout(Timeout wheelTimeout) {
		this.wheelTimeout = wheelTimeout;
	}

	public NnRound getNnround() {
		return nnround;
	}

	public void setNnround(NnRound nnround) {
		this.nnround = nnround;
	}
   
}

	
